package com.company;

import java.io.*;
import java.net.*;
import java.util.concurrent.*;
import java.util.*;

public class starCentralServer {
    private static final int PORT = 12345;
    private static final List<String> activeGameServers = Collections.synchronizedList(new ArrayList<>());
    private static  ArrayList<String> ungreArr = new ArrayList<>();
    private static ExecutorService pool = Executors.newCachedThreadPool();


    public static void main(String[] args) throws IOException {
        try (ServerSocket serverSocket = new ServerSocket(PORT)) {
            System.out.println("Central Server is running on port: " + PORT);

            while (true) {
                try {
                    Socket clientSocket = serverSocket.accept();
                    pool.execute(new ClientHandler(clientSocket));
                } catch (IOException ex) {
                    System.out.println("Error accepting connection.");
                    ex.printStackTrace();
                }
            }
        }
    }

    private static class ClientHandler implements Runnable {
        private final Socket clientSocket;

        public ClientHandler(Socket socket) {
            this.clientSocket = socket;
        }

        @Override
        public void run() {
            try (BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                 PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {

                String request = in.readLine();
                System.out.println(request);
                if ("GET_SERVER_LIST".equals(request)) {
                    activeGameServers.forEach(out::println);
                } else if (request.startsWith("REGISTER")) {
                    String serverInfo = request.substring("REGISTER".length()).trim();
                    activeGameServers.add(serverInfo);
                    System.out.println("Registered new game server: " + serverInfo);
                } else if (request.startsWith("UNREGISTER")) {
                    String serverInfo = request.substring("UNREGISTER".length()).trim();
                    ungreArr.add(serverInfo);
                    int count = 0;
                    for (int i = 0; i < ungreArr.size(); i++) {


                        if (serverInfo.equals(ungreArr.get(i)))
                        {
                            count++;
                        }
                    }
                    if (count==2)
                    {
                        activeGameServers.remove(serverInfo);
                        System.out.println("Unregistered game server: " + serverInfo);
                        for (int i = 0; i < ungreArr.size(); i++) {
                            if (serverInfo.equals(ungreArr.get(i)))
                            {
                                ungreArr.remove(i);
                            }
                        }
                    }
                }
            } catch (IOException ex) {
                System.out.println("Error communicating with the client.");
                ex.printStackTrace();
            }
        }
    }
}